<?php
/*************************************************************************************
   Copyright notice

   This script uses some code parts of the glossary module for phpwcms (c) Oliver Georgi
   (c) 2002-2009 Oliver Georgi (oliver@phpwcms.de) // All rights reserved.

   This script uses some code parts from bmcatalog module
   (c) 2010 Benjamin Matzmorr, www.etype.de

   (c) 2012 webrealisierung gmbh, www.webrealisierung.ch
   Catalogue Module V 1.0.0  -  Date: 2012-06-01

   The GNU General Public License can be found at http://www.gnu.org/copyleft/gpl.html
   A copy is found in the textfile GPL.txt and important notices to the license
   from the author is found in LICENSE.txt distributed with these scripts.

   This script is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
   PARTICULAR PURPOSE.  See the GNU General Public License for more details.

   This copyright notice MUST APPEAR in all copies of the script!
*************************************************************************************/
// ----------------------------------------------------------------
// obligate check for phpwcms constants
if (!defined('PHPWCMS_ROOT')) {
   die("You Cannot Access This Script Directly, Have a Nice Day.");
}
// ----------------------------------------------------------------


/*
 * module Catalogue
 * ================
 *
 * some defaults for modules: $phpwcms['modules'][$module]
 * store all related in here and holds some default values
 * ['path'], ['type'], ['name']
 * language values are store in $BL['modules'][$module]
 * as defined in lang/en.lang.php
 * but maybe to keep default language file more lightweight
 * you can use own language definitions starting within this file
 *
 */

// first check if exists
if(isset($phpwcms['modules'][$module]['path'])) {
	// put translation back to have easier access to it - use it as relation
	$BLM = & $BL['modules'][$module];
  define('MODULE_HREF', 'phpwcms.php?do=modules&amp;module='.$module); //like: phpwcms.php?do=modules&module=br_catalogue
  define('MODULE_CTG_DIR', $phpwcms['modules'][$module]['dir']); //like: include/inc_module/mod_catalogue/
  define('MODULE_CTG_PATH', $phpwcms['modules'][$module]['path']); //like: /home/.../include/inc_module/mod_catalogue/
  //get functions for module (backend nodule section only)
  include_once($phpwcms['modules'][$module]['path'].'inc/ctg.functions.inc.php');
  include_once($phpwcms['modules'][$module]['path'].'inc/ctg.classes.php');
//	$BE['HEADER']['module_calendar.css'] = '';

  $sql = "CREATE TABLE IF NOT EXISTS `".DB_PREPEND."phpwcms_mod_catalogue_entry` (
  `ctge_id` int(11) NOT NULL auto_increment,
  `ctge_catalogid` text NOT NULL,
  `ctge_tags` text NOT NULL,
  `ctge_title` varchar(255) NOT NULL,
  `ctge_title2` varchar(255) NOT NULL,
  `ctge_text` text NOT NULL,
  `ctge_text2` text NOT NULL,
  `ctge_created` datetime NOT NULL,
  `ctge_changed` datetime NOT NULL,
  `ctge_image` text NOT NULL,
  `ctge_file` text NOT NULL,
  `ctge_address` text NOT NULL,
  `ctge_address_address` varchar(128) NOT NULL,
  `ctge_address_city` varchar(128) NOT NULL,
  `ctge_address_zip` varchar(20) NOT NULL,
  `ctge_address_country` varchar(128) NOT NULL,
  `ctge_address_long` double NOT NULL,
  `ctge_address_lat` double NOT NULL,
  `ctge_address_marker` varchar(128) NOT NULL,
  `ctge_address_show_map` tinyint(4) NOT NULL default '0',
  `ctge_userId` int(11) NOT NULL default '0',
  `ctge_status` tinyint(4) NOT NULL default '0' COMMENT 'hide=0; show=1; 9=deleted',
  PRIMARY KEY  (`ctge_id`)
) ENGINE=MyISAM"._dbGetCreateCharsetCollation();


  // first check if neccessary db-table exists
  if (mysql_num_rows(mysql_query("SHOW TABLES FROM `" . $GLOBALS['phpwcms']['db_table'] . "` LIKE '%phpwcms_mod_catalogue_entry'"))) {

  //container var for all plugin activities
	$plugin = array();

  //where to?
	$controller	= empty($_GET['controller']) ? 'entry' : strtolower($_GET['controller']);

	if(isset($_GET['edit'])) {
		$action	= 'edit';
	} elseif(isset($_GET['update'])) {
		$action	= 'update';
	} elseif(isset($_GET['verify'])) {
		$action	= 'status';
	} elseif(isset($_GET['upload'])) {
		$action	= 'upload';
	} elseif(isset($_GET['delete'])) {
		$action	= 'delete';
  } else {
		$action		= '';
	}

	switch($controller) {
		case 'module':	$controller	= 'module';
    				break;
		case 'catalog':	$controller	= 'catalog';
						break;
		case 'entry':	$controller	= 'entry';
						break;
		case 'lang':	$controller	= 'lang';
						break;
		case 'about':	$controller	= 'about';
						break;
		default:		$controller	= 'entry';
	}

		// processing
	if( $action ) {
		include_once($phpwcms['modules'][$module]['path'].'inc/processing.' . $controller . '.inc.php');
	}

	// header
	include_once($phpwcms['modules'][$module]['path'].'inc/tabs.inc.php');

	// listing
	if($action) {
		include_once($phpwcms['modules'][$module]['path'].'inc/'.$action.'.' . $controller . '.inc.php');
  } else {
		include_once($phpwcms['modules'][$module]['path'].'inc/listing.' . $controller . '.inc.php');
	}

  } else if (_dbQuery($sql, 'CREATE')) { //create table


    $sql_ctg = "CREATE TABLE IF NOT EXISTS `".DB_PREPEND."phpwcms_mod_catalogue_catalog` (
  `ctgc_id` int(11) NOT NULL auto_increment,
  `ctgc_created` datetime NOT NULL,
  `ctgc_changed` datetime NOT NULL,
  `ctgc_name` varchar(255) NOT NULL,
  `ctgc_status` int(1) NOT NULL default '0',
  PRIMARY KEY  (`ctgc_id`)
) ENGINE=MyISAM"._dbGetCreateCharsetCollation();

    $sql_error = array();

    //create db tables

    if(!_dbQuery($sql_ctg, 'CREATE')) {

    	$sql_error[1] = '<p class="error">Error creating <b>phpwcms_mod_catalogue_catalog</b> initial database table: '.@htmlentities(@mysql_error(), ENT_QUOTES, PHPWCMS_CHARSET).'</p>';

    }


      echo '<p class="title">Catalogue setup</p>';
      if(count($sql_error)) {
      	echo implode(LF, $sql_error);
      } else {
        echo '<p class="">Catalogue setup successful. Please click the module link again to start working with the module!</p>';
      }

  } else { //not good
      	echo '<p>'.@htmlentities(@mysql_error(), ENT_QUOTES, PHPWCMS_CHARSET).'</p>';
  }

}
?>